<?php

// Self wrote of the load_person part
$fp = fopen("pinfo.csv","r");
$people_lst = array();
$maximum_com = array();// this is actually no need
$line = fgetcsv($fp); // skip the first line
while($line = fgetcsv($fp)){
  $people_lst[$line[2]] = array(
    'fullname' => $line[0],
    'pid' => $line[1],
    'pyid' => $line[2],
  );
}

// get a list of pids for person reference
$pid_to_pyid = array();
foreach($people_lst as $person){
  $pid_to_pyid[$person['pid']] = $person['pyid'];
}
$pid_lst = array_keys($pid_to_pyid);

$fp = fopen("ginfo.csv","r");
$line = fgetcsv($fp); // skip the first line
// and randomly assign a chair_pid
$group_lst = array();
while($line = fgetcsv($fp)){
  $group_lst[$line[2]] = array(
    'name' => $line[0],
    'gid' => $line[1],
    'gyid' => $line[2],
    'quota' => $line[4],
    // generate a random chair_pid
    'chair_pid' => $pid_lst[rand(0,sizeof($pid_lst))],
  );
}

/*  Test of the Chair Person
foreach($group_lst as $group){
  printf("Chair person id is: %s\n",
    $people_lst[$pid_to_pyid[$group['chair_pid']]]['fullname']);
}
*/

/* These data need generation and save:
    1. group_exclude_lst -- data format:
      gyid [pyid] (order doesn't matter)
    2. group_rank_people (200% of the quota)
      gyid num [pyid] (order is the rank)
 */

$pyids = array_keys($people_lst);
$parr_siz = sizeof($pyids);

// initally, some people are already in the group -- gen $group_current_member info
$group_current_member = array();
foreach($group_lst as $gyid=>$group){
  shuffle($pyids);
  $group_current_member[$gyid] = array_slice($pyids,0,rand(0,1)); // just a few members already in group
}
// then save to a file
$fp = fopen("group_current_member.txt","w");
foreach($group_current_member as $gyid => $lst){
  fprintf($fp,"%d",$gyid);
  foreach($lst as $pyid){
    fprintf($fp," %d",$pyid);
  }
  fprintf($fp,"\n");
}
fclose($fp);


// gen group_exclude_lst
$group_exclude_lst = array();
foreach($group_lst as $gyid=>$group){
  shuffle($pyids);
  $arr =  array_slice($pyids,0,rand(0,(int)($parr_siz/3)));
  foreach($arr as $pyid){
    if(in_array($pyid,$group_current_member[$gyid]))  continue;
    $group_exclude_lst[$gyid][] = $pyid;
  }
}
// then save to a file
$fp = fopen("exclude_lst.txt","w");
foreach($group_exclude_lst as $gyid => $lst){
  fprintf($fp,"%d",$gyid);
  foreach($lst as $pyid){
    fprintf($fp," %d",$pyid);
  }
  fprintf($fp,"\n");
}
fclose($fp);


// gen group pref_lst, with person not from the group, up to 2* group's quota
$group_pref = array();
foreach($group_lst as $gyid =>$group){
  shuffle($pyids); $i = 0; $cnt = 0;
  while($cnt < 2*$group['quota'] && $i < $parr_siz){
    $current_pyid = $pyids[$i++];
    if( (!empty($group_exclude_lst[$gyid]) && 
        in_array($current_pyid,$group_exclude_lst[$gyid]))
     || (!empty($group_current_member[$gyid]) &&
        in_array($current_pyid,$group_current_member[$gyid]))
        )  continue; // if excluded then not prefered.
    

    // if not excluded, can take this one
    $group_pref[$gyid][] = $current_pyid;
  }
}

// then save to a file
$fp = fopen("group_pref.txt","w");
foreach($group_pref as $gyid => $lst){
  fprintf($fp,"%d",$gyid);
  foreach($lst as $pyid){
    fprintf($fp," %d",$pyid);
  }
  fprintf($fp,"\n");
}
fclose($fp);

// gen person pref here
$gyids = array_keys($group_lst);
$garr_siz = sizeof($gyids);
$people_pref = array();
foreach($people_lst as $pyid => $people){
  shuffle($gyids);
  $people_pref[$pyid] = array_slice($gyids,0,rand(0,$garr_siz-1));
}
// and.. save the file :)

// then save to a file
$fp = fopen("people_pref.txt","w");
foreach($people_pref as $pyid => $lst){
  fprintf($fp,"%d",$pyid);
  foreach($lst as $gyid){
    fprintf($fp," %d",$gyid);
  }
  fprintf($fp,"\n");
}
fclose($fp);

print("Done.\n");
